#include "PMU_Disp.h"
#include "Drv_W2812.h"
#include "PMU_Task.h"

uint8_t PmuPowerOnPlayOver = 0;
uint8_t rgbBuf[WS2812_LEDNUM][3] = {0};

static void setPixel(uint8_t num, uint8_t red, uint8_t green, uint8_t blue)
{
    if (num < WS2812_LEDNUM) {
        rgbBuf[num][0] = red;
        rgbBuf[num][1] = green;
        rgbBuf[num][2] = blue;
    } else if (num == WS2812_LEDNUM) {
        setPixel(0, red, green, blue);
        setPixel(1, red, green, blue);
        setPixel(2, red, green, blue);
        setPixel(3, red, green, blue);
    }
}

void pmuDispPowerOnTask(const uint16_t dT_ms)
{
    static uint8_t _step = 0;
    static uint16_t _keydowntime = 0;

    //if (!PmuData.powerKeyIsDown) {
	if (0) {
        if (_step != 0) {
            setPixel(WS2812_LEDNUM, 0, 0, 0);
        }

        _step = 0;
        _keydowntime = 0;
        return;
    } else if (_keydowntime < 5000) {
        _keydowntime += dT_ms;
    }

    switch (_step) {
    case 0: {
        setPixel(WS2812_LEDNUM, 0, 0, 0);
        _step++;
        _keydowntime = 0;
    }
    break;

    case 1: {
        if (_keydowntime >= 300) {
            setPixel(0, 50, 50, 50);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 2: {
        if (_keydowntime >= 300) {
            setPixel(1, 50, 50, 50);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 3: {
        if (_keydowntime >= 300) {
            setPixel(2, 50, 50, 50);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 4: {
        if (_keydowntime >= 300) {
            setPixel(3, 50, 50, 50);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 5: {
        if (_keydowntime >= 300) {
            setPixel(WS2812_LEDNUM, 0, 50, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 6: {
        if (_keydowntime >= 300) {
            setPixel(WS2812_LEDNUM, 0, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 7: {
        PmuData.sysPowerOn = 1;
    }
    break;

    default: {
        _keydowntime = 0;
        _step = 0;
    }
    break;
    }
}

static uint8_t pmuDispPowerOffTask(const uint16_t dT_ms)
{
    static uint8_t _step = 0;
    static uint16_t _keydowntime = 0;

    if (!PmuData.powerKeyIsDown) {
        if (_step != 0) {
            setPixel(WS2812_LEDNUM, 0, 0, 0);
        }

        _step = 0;
        _keydowntime = 0;
        return 0;
    } else if (_keydowntime < 5000) {
        _keydowntime += dT_ms;
    }

    switch (_step) {
    case 0: {
        if (_keydowntime > 1000) {
            setPixel(WS2812_LEDNUM, 50, 50, 50);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 1: {
        if (_keydowntime >= 300) {
            setPixel(3, 0, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 2: {
        if (_keydowntime >= 300) {
            setPixel(2, 0, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 3: {
        if (_keydowntime >= 300) {
            setPixel(1, 0, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 4: {
        if (_keydowntime >= 300) {
            setPixel(0, 0, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 5: {
        if (_keydowntime > 300) {
            setPixel(WS2812_LEDNUM, 50, 0, 0);
            _step++;
            _keydowntime = 0;
        }
    }
    break;

    case 6: {
        PmuData.sysPowerOff = 1;
    }
    break;

    default: {
        _step = 0;
        _keydowntime = 0;
    }
    break;
    }

    if (_step > 0) {
        return 1;
    } else {
        return 0;
    }
}
static void pmuDispVbatTask(const uint16_t dT_ms)
{
	static uint8_t _vbatStep = 0;
	static uint16_t _vbatDisptime = 0;
	 switch (PmuData.vbat_state) {
		case 0: {
            _vbatDisptime+=dT_ms;
			if (_vbatDisptime > 200) {
                if(_vbatStep == 0) {
                    setPixel(WS2812_LEDNUM, 50, 0, 0);
                    _vbatStep = 1;
                }
                else
                {
                    setPixel(WS2812_LEDNUM, 0, 0, 0);
                    _vbatStep = 0;
                }
				_vbatDisptime = 0;
			}
        }
        break;
			case 1: {
			    setPixel(WS2812_LEDNUM, 50, 0, 0);
        }
            break;
			case 2: {
				setPixel(WS2812_LEDNUM, 50, 50, 0);
        }
            break;
			case 3: {
				setPixel(WS2812_LEDNUM, 50, 50, 50);
        }
            break;        
			case 4: {
				setPixel(WS2812_LEDNUM, 0, 50, 0);
            break;
		}
            default: {
        _vbatStep = 0;
        _vbatDisptime = 0;
        }
        break;
	}
}
static void pmuDispNormalTask(const uint16_t dT_ms)
{
	pmuDispVbatTask(dT_ms);
}

void PmuDispTask(const uint16_t dT_ms)
{
    switch (PmuData.runState) {
    case 0: {
        pmuDispPowerOnTask(dT_ms);
    }
    break;

    case 1: {
    }
    break;

    case 2: {
        if (!pmuDispPowerOffTask(dT_ms)) {
            pmuDispNormalTask(10);
        }
    }
    break;

    case 3: {
        setPixel(WS2812_LEDNUM, 50, 0, 0);
    }
    break;

    default: {
    }
    break;
    }

    DrvWs2812Updata(rgbBuf, WS2812_LEDNUM);
}
